var express = require("express");
var app = express();
var request = require("request");
var bodyParser = require("body-parser");

app.use(express.static("public"));
// 创建 application/json 解析
var jsonParser = bodyParser.json();
// 创建 application/x-www-form-urlencoded 解析
var urlencodedParser = bodyParser.urlencoded({ extended: false });

app.all("*", function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"
  );
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", " 3.2.1");
  if (req.method == "OPTIONS") res.send(200);
  /*让options请求快速返回*/ else next();
});
//基础的api路径 baseURL
var baseURL = "http://api2.nahuo.com/v3/pinhuoitem/GetHomeActivityList2/?";
//get :请求内容放在请求链接内
//post:放到一个对象里
//http://localhost:3000/api/jingxuan?
//req: 存储了请求信息,请求的内容会放置到req的query属性Hong Kong
app.get("/api/home", function(req, res) {
  var cid = req.query.cid;
  var debug = req.query.debug;
  var pageIndex = req.query.pageIndex;
  var pageSize = req.query.pageSize;
  var url = `${baseURL}cid=${cid}&debug=${debug}&pageIndex=${pageIndex}&pageSize=${pageSize}`;
  // 在node服务器中向其他的服务器发起请求
  console.log(url);
  request(url, function(error, response, body) {
    res.send(body);
  });
});

// 轮播图
//基础的api路径 baseURL
var focusURL = "http://api2.nahuo.com/v3/ads/GetBannersFormTypeV2/?";
app.get("/api/focus", function(req, res) {
  var AreaTypeID = req.query.AreaTypeID;
  var valueID = req.query.valueID;
  var url = `${focusURL}AreaTypeID=${AreaTypeID}&valueID=${valueID}`;
  // 在node服务器中向其他的服务器发起请求
  console.log(url);
  request(url, function(error, response, body) {
    res.send(body);
  });
});
//分类页面
app.get("/api/category", function(req, res) {
  var categoryURL =
    "https://mrobot.pconline.com.cn/s-3600/best/search/classfyIndex.xsp?type=1&platform=ios&v=3.3.0&inReview=0";
  request(categoryURL, function(error, response, body) {
    res.send(body);
  });
});

//搜索功能
app.get("/api/searchview", function (req, res) {
  var searchURL = "http://api2.nahuo.com/v3/shop/agent/getitemhotkeyword?fromType=3&userIDs=-1&version=2";
  // console.log(usearchValue);
  request(searchURL, function (error, response, body) {
    res.send(body);
  });
});
app.get("/api/searchview/searchkeyword", function (req, res) {
  var mainurl = "http://api2.nahuo.com/v3/pinhuoitem/searchv2?";
  var displayMode = req.query.displayMode;
  var keyword = req.query.keyword;
  if (displayMode == 0) {
    var keyword = encodeURI(keyword);
  }
  var pageIndex = req.query.pageIndex;
  var pageSize = req.query.pageSize;
  var url = `${mainurl}displayMode=${displayMode}&keyword=${keyword}&pageIndex=${pageIndex}&pageSize=${pageSize}`;
  request(url, function (error, response, body) {
    console.log(url);
    res.send(body);
  });
});


//约拼   http://api2.nahuo.com/v3/xiaozu/topic/list_v2/60033/1/20?
app.get("/api/yuepin", function(req, res) {
  var yuepinURL = "http://api2.nahuo.com/v3/xiaozu/topic/list_v2/60033/1/20?";
  request(yuepinURL, function(error, response, body) {
    res.send(body);
  });
});
//http://api2.nahuo.com/v3/xiaozu/group/60033
app.get("/api/title", function(req, res) {
  var titleURL = "http://api2.nahuo.com/v3/xiaozu/group/60033";
  request(titleURL, function(error, response, body) {
    res.send(body);
  });
});

// post请求和get请求在服务器端的处理方式是不一样的,所以需要特殊的插件来
// 处理post请求,post请求的数据会存储到req.body对象里
app.post("/login", urlencodedParser, function(req, res) {
  console.log(req.body);
  var username = req.body.username;
  var password = req.body.password;
  var responseData = {
    username: username,
    message: "请求成功",
    icon:
      "https://i7.3conline.com/images/upload/upc/face/41/94/03/10/41940310_100x100"
  };
  if (username == "admin" && password == "123") {
    res.send(responseData);
  }
});

app.post("/register", urlencodedParser, function(req, res) {
  var mobile = req.body.phoneNum;
  var password = req.body.password;
  // mobile=15362950319&mobileVCode=063583&noAccountName=1
  // &password=zmt12341234&passwordConfirm=zmt12341234&username=15362950319
  // https://passport3.pconline.com.cn/passport3/api/registerForMobile.jsp?req_enc=utf-8&resp_enc=utf-8
  // var url = "https://passport3.pconline.com.cn/passport3/api/registerForMobile.jsp?req_enc=utf-8&resp_enc=utf-8";
  // var userInfo = JSON.stringify({mobile: mobile,password: password});
  // console.log(userInfo);
  // request(url,userInfo,function(error,response,body){
  //     console.log(body);
  // })
});
app.get("/", function(req, res) {
  res.sendFile(__dirname + "/index.html");
});
app.listen(3000, function(req, res) {
  console.log("在浏览器中输入 http://localhost:3000");
});
